home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / poly3d-h / poly3d-h.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-19  |  12.2 KB  |  331 lines

  1. /*****************************************************************************
  2. *   Program to draw 3D object as wireframe after removing the hidden lines.  *
  3. * This porgram works in object space, and if redirect stdout to a file, dump *
  4. * the visible polylines into it instead of drawing them on current device.   *
  5. * This may be used to display the results on any device (a plotter !?) later.*
  6. *                                         *
  7. * Written by:  Gershon Elber                Ver 3.0, Aug. 1990   *
  8. *****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include "program.h"
  13. #include "getarg.h"
  14. #include "genmat.h"
  15. #include "config.h"
  16. #include "ip_cnvrt.h"
  17.  
  18. #ifdef NO_CONCAT_STR
  19. static char *VersionStr =
  20.     "Poly3D-H    Version 5.0,    Gershon Elber,\n\
  21.     (C) Copyright 1989/90-95 Gershon Elber, Non commercial use only.";
  22. #else
  23. static char *VersionStr = "Poly3D-H    " VERSION ",    Gershon Elber,    "
  24.     __DATE__ ",  " __TIME__ "\n" COPYRIGHT ", Non commercial use only.";
  25. #endif /* NO_CONCAT_STR */
  26.  
  27. static char *CtrlStr =
  28. #ifdef DOUBLE
  29.     "poly3d-h b%- m%- i%- e%-#Edges!d H%- 4%- W%-Width!F F%-PolyOpti|FineNess!d!F q%- o%-OutName!s c%- z%- DFiles!*s";
  30. #else
  31.     "poly3d-h b%- m%- i%- e%-#Edges!d H%- 4%- W%-Width!f F%-PolyOpti|FineNess!d!f q%- o%-OutName!s c%- z%- DFiles!*s";
  32. #endif /* DOUBLE */
  33.  
  34. int NumOfPolygons = 0;              /* Total number of polygons to handle. */
  35. MatrixType GlblViewMat;                  /* Current view of object. */
  36.  
  37. /* Data structures used by the hidden line modules: */
  38. int EdgeCount = 0;
  39. EdgeStruct *EdgeHashTable[EDGE_HASH_TABLE_SIZE];
  40. IPPolygonStruct *PolyHashTable[POLY_HASH_TABLE_SIZE];
  41.  
  42. /* The following are setable variables (via configuration file poly3d-h.cfg).*/
  43. int GlblMore = FALSE,
  44.     GlblClipScreen = TRUE,
  45.     GlblFourPerFlat = FALSE,
  46.     GlblOptimalPolyApprox = FALSE,
  47.     GlblQuiet = FALSE,
  48.     GlblOutputHasRGB = FALSE,
  49.     GlblOutputRGB[3] = { 255, 255, 255 },
  50.     GlblOutputColor = VISIBLE_COLOR,
  51.     GlblNumEdge = 0,
  52.     GlblBackFacing = FALSE,
  53.     GlblInternal = FALSE,
  54.     GlblOutputHiddenData = FALSE;
  55.  
  56. RealType
  57.     GlblFineNess = DEFAULT_FINENESS,
  58.     GlblOutputWidth = VISIBLE_WIDTH;
  59.  
  60. static ConfigStruct SetUp[] =
  61. {
  62.   { "Internal",       "-i", (VoidPtr) &GlblInternal,    SU_BOOLEAN_TYPE },
  63.   { "BackFacing",  "-b", (VoidPtr) &GlblBackFacing,    SU_BOOLEAN_TYPE },
  64.   { "More",       "-m", (VoidPtr) &GlblMore,        SU_BOOLEAN_TYPE },
  65.   { "ClipScreen",  "-c", (VoidPtr) &GlblClipScreen,    SU_BOOLEAN_TYPE },
  66.   { "Quiet",       "-q", (VoidPtr) &GlblQuiet,        SU_BOOLEAN_TYPE },
  67.   { "FourPerFlat", "-4", (VoidPtr) &GlblFourPerFlat,    SU_BOOLEAN_TYPE },
  68.   { "DumpHidden",  "-H", (VoidPtr) &GlblOutputHiddenData,SU_BOOLEAN_TYPE },
  69.   { "PolyOpti",    "-F", (VoidPtr) &GlblOptimalPolyApprox,SU_INTEGER_TYPE },
  70.   { "NumOfEdges",  "-e", (VoidPtr) &GlblNumEdge,    SU_INTEGER_TYPE },
  71.   { "LineWidth",   "-W", (VoidPtr) &GlblOutputWidth,    SU_REAL_TYPE },
  72.   { "FineNess",       "-F", (VoidPtr) &GlblFineNess,    SU_REAL_TYPE }
  73. };
  74. #define NUM_SET_UP    (sizeof(SetUp) / sizeof(ConfigStruct))
  75.  
  76. /*****************************************************************************
  77. * DESCRIPTION:                                                               M
  78. * Main module of poly3d-h - Read command line and do what is needed...         M
  79. *                                                                            *
  80. * PARAMETERS:                                                                M
  81. *   argc, argv:  Command line.                                               M
  82. *                                                                            *
  83. * RETURN VALUE:                                                              M
  84. *   void                                                                     M
  85. *                                                                            *
  86. * KEYWORDS:                                                                  M
  87. *   main                                                                     M
  88. *****************************************************************************/
  89. void main(int argc, char **argv)
  90. {
  91.     int Error,
  92.     EdgesFlag = FALSE,
  93.     VerFlag = FALSE,
  94.     OutFlag = FALSE,
  95.     WidthFlag = FALSE,
  96.     NumFiles = FALSE,
  97.     OptPolyApproxFlag = FALSE;
  98.     char
  99.     *OutFileName = NULL,
  100.     **FileNames = NULL;
  101.     FILE *OutFile;
  102.     IPObjectStruct *PObjects, *PTmpObj;
  103.  
  104.     Config("poly3d-h", SetUp, NUM_SET_UP);   /* Read config. file if exists. */
  105.  
  106.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  107.                &GlblBackFacing, &GlblMore, &GlblInternal,
  108.                &EdgesFlag, &GlblNumEdge, &GlblOutputHiddenData,
  109.                &GlblFourPerFlat, &WidthFlag,
  110.                &GlblOutputWidth, &OptPolyApproxFlag,
  111.                &GlblOptimalPolyApprox, &GlblFineNess,
  112.                &GlblQuiet, &OutFlag, &OutFileName,
  113.                &GlblClipScreen, &VerFlag, &NumFiles,
  114.                &FileNames)) != 0) {
  115.     GAPrintErrMsg(Error);
  116.     GAPrintHowTo(CtrlStr);
  117.     Poly3dhExit(1);
  118.     }
  119.  
  120.     if (VerFlag) {
  121.     fprintf(stderr, "\n%s\n\n", VersionStr);
  122.     GAPrintHowTo(CtrlStr);
  123.     ConfigPrint(SetUp, NUM_SET_UP);
  124.     Poly3dhExit(0);
  125.     }
  126.  
  127.     if (!NumFiles) {
  128.     fprintf(stderr, "No data file names were given, exit\n");
  129.     GAPrintHowTo(CtrlStr);
  130.     Poly3dhExit(1);
  131.     }
  132.  
  133.     if (GlblQuiet)
  134.     GlblMore = FALSE;
  135.  
  136.     if (GlblOutputHiddenData && GlblBackFacing) {
  137.     fprintf(stderr, "Warning: You have activated both output of hidden data (-H) and back facing\n");
  138.     fprintf(stderr, "\t elimination (-b). Chances are you did not want to do that.\n");
  139.     }
  140.  
  141.     /* Get the data files: */
  142.     if ((PObjects = IritPrsrGetDataFiles(FileNames, NumFiles, TRUE,
  143.                      GlblMore)) == NULL)
  144.     Poly3dhExit(1);
  145.  
  146.     /* If has color/width attribute in geometries - uses them. Assume */
  147.     /* all objects share the same attribute (should be fixed).        */
  148.     for (PTmpObj = PObjects; PTmpObj != NULL; PTmpObj = PTmpObj -> Pnext) {
  149.     if (IP_IS_GEOM_OBJ(PTmpObj)) {
  150.         int Color;
  151.         RealType Width;
  152.  
  153.         if ((Width = AttrGetObjectRealAttrib(PTmpObj, "width")) <
  154.                             IP_ATTR_BAD_REAL)
  155.         GlblOutputWidth = Width;
  156.  
  157.         if (!GlblOutputHasRGB)
  158.         GlblOutputHasRGB = AttrGetObjectRGBColor(PTmpObj,
  159.                              &GlblOutputRGB[0],
  160.                              &GlblOutputRGB[1],
  161.                              &GlblOutputRGB[2]);
  162.         if ((Color = AttrGetObjectColor(PTmpObj)) != IP_ATTR_NO_COLOR)
  163.         GlblOutputColor = Color;
  164.     }
  165.     }
  166.  
  167.     /* And update the global viewing matrix: */
  168.     if (IritPrsrWasPrspMat)
  169.     MatMultTwo4by4(GlblViewMat, IritPrsrViewMat, IritPrsrPrspMat);
  170.     else
  171.     GEN_COPY(GlblViewMat, IritPrsrViewMat, sizeof(MatrixType));
  172.  
  173.     /* Prepare data structures to be able to decide on visibility: */
  174.     PrepareViewData(PObjects);
  175.  
  176.     if (OutFlag) {
  177.     if ((OutFile = fopen(OutFileName, "w")) == NULL) {
  178.         fprintf(stderr, "Failed to open \"%s\".\n", OutFileName);
  179.         Poly3dhExit(2);
  180.     }
  181.     }
  182.     else
  183.     OutFile = stdout;
  184.  
  185.     OutVisibleEdges(OutFile);           /* Scan all sub-edges output visible. */
  186.  
  187.     if (OutFile != stdout)
  188.     fclose(OutFile);
  189.  
  190.     Poly3dhExit(0);
  191. }
  192.  
  193. /*****************************************************************************
  194. * DESCRIPTION:                                                               M
  195. * Routine to convert all surfaces/curves into polylines as follows:         M
  196. *   Curve is converted to a single polyline with SamplesPerCurve samples.    M
  197. *   Surface is converted into GlblNumOfIsolines curves in each axes, each    M
  198. * handled as Curve above. The original curves and surfaces are then deleted. M
  199. *   This function is a call back function of the irit parser.             M
  200. *                                                                            *
  201. * PARAMETERS:                                                                M
  202. *   FreeForms:  Crvs/Srfs/Trimmed Srfs/Trivariates read from a file by the   M
  203. *               irit parser.                                 M
  204. *                                                                            *
  205. * RETURN VALUE:                                                              M
  206. *   IPObjectStruct *:   Processed freeform geometry. This function simply    M
  207. *                       returns what it gots.                                M
  208. *                                                                            *
  209. * KEYWORDS:                                                                  M
  210. *   IritPrsrProcessFreeForm, conversion                                      M
  211. *****************************************************************************/
  212. IPObjectStruct *IritPrsrProcessFreeForm(IritPrsrFreeFormStruct *FreeForms)
  213. {
  214.     CagdCrvStruct *Crv, *Crvs;
  215.     CagdSrfStruct *Srf, *Srfs;
  216.     IPObjectStruct *PObj,
  217.     *CrvObjs = FreeForms -> CrvObjs,
  218.     *SrfObjs = FreeForms -> SrfObjs;
  219.     IPPolygonStruct *PPolygon, *PPolygonTemp;
  220.  
  221.     if (CrvObjs == NULL && SrfObjs == NULL)
  222.     return NULL;
  223.  
  224.     /* Make sure requested format is something reasonable. */
  225.     if (GlblOptimalPolyApprox == 0 && GlblFineNess < 2) {
  226.     GlblFineNess = 2;
  227.     if (GlblMore)
  228.         fprintf(stderr, "FineNess is less than 2, 2 picked instead.\n");
  229.     }
  230.  
  231.     if (CrvObjs) {
  232.     for (PObj = CrvObjs; PObj != NULL; PObj = PObj -> Pnext) {
  233.         if (GlblMore)
  234.         fprintf(stderr, "Processing surface object \"%s\".\n", 
  235.             PObj -> Name);
  236.         Crvs = PObj -> U.Crvs;
  237.         PObj -> U.Pl = NULL;
  238.         PObj -> ObjType = IP_OBJ_POLY;
  239.         IP_SET_POLYLINE_OBJ(PObj);
  240.         for (Crv = Crvs; Crv != NULL; Crv = Crv -> Pnext) {
  241.         PPolygon = PPolygonTemp =
  242.             IritCurve2Polylines(Crv, (int) GlblFineNess, FALSE);
  243.  
  244.         if (PPolygon != NULL) {
  245.             while (PPolygonTemp -> Pnext)
  246.             PPolygonTemp = PPolygonTemp -> Pnext;
  247.             PPolygonTemp -> Pnext = PObj -> U.Pl;
  248.             PObj -> U.Pl = PPolygon;
  249.         }
  250.         }
  251.         CagdCrvFreeList(Crvs);
  252.     }
  253.     }
  254.  
  255.     if (SrfObjs) {
  256.     for (PObj = SrfObjs; PObj != NULL; PObj = PObj -> Pnext) {
  257.         if (GlblMore)
  258.         fprintf(stderr, "Processing surface object \"%s\".\n", 
  259.             PObj -> Name);
  260.         Srfs = PObj -> U.Srfs;
  261.         PObj -> U.Pl = NULL;
  262.         PObj -> ObjType = IP_OBJ_POLY;
  263.         IP_SET_POLYGON_OBJ(PObj);
  264.         for (Srf = Srfs; Srf != NULL; Srf = Srf -> Pnext) {
  265.         PPolygon = PPolygonTemp =
  266.             IritSurface2Polygons(Srf, GlblFourPerFlat, GlblFineNess,
  267.                      FALSE, GlblOptimalPolyApprox);
  268.  
  269.         if (PPolygon != NULL) {
  270.             while (PPolygonTemp -> Pnext)
  271.             PPolygonTemp = PPolygonTemp -> Pnext;
  272.             PPolygonTemp -> Pnext = PObj -> U.Pl;
  273.             PObj -> U.Pl = PPolygon;
  274.         }
  275.         }
  276.         CagdSrfFreeList(Srfs);
  277.     }
  278.     }
  279.  
  280.     if (SrfObjs == NULL)
  281.     return CrvObjs;
  282.     else if (CrvObjs == NULL)
  283.     return SrfObjs;
  284.     else {
  285.     for (PObj = SrfObjs; PObj -> Pnext != NULL; PObj = PObj -> Pnext);
  286.     PObj -> Pnext = CrvObjs;
  287.     return SrfObjs;
  288.     }
  289. }
  290.  
  291. /*****************************************************************************
  292. * DESCRIPTION:                                                               M
  293. * Poly3d-h Exit routine.                             M
  294. *                                                                            *
  295. * PARAMETERS:                                                                M
  296. *   ExitCode:    To notify O.S. with result of program.                      M
  297. *                                                                            *
  298. * RETURN VALUE:                                                              M
  299. *   void                                                                     M
  300. *                                                                            *
  301. * KEYWORDS:                                                                  M
  302. *   Poly3dhExit                                                              M
  303. *****************************************************************************/
  304. void Poly3dhExit(int ExitCode)
  305. {
  306.     if (!GlblQuiet)
  307.     fprintf(stderr, "\n");
  308.  
  309.     exit(ExitCode);
  310. }
  311.  
  312. #ifdef DEBUG
  313.  
  314. /*****************************************************************************
  315. * DESCRIPTION:                                                               *
  316. *    Dummy function to link at debugging time.                               *
  317. *                                                                            *
  318. * PARAMETERS:                                                                *
  319. *                                                                            *
  320. * RETURN VALUE:                                                              *
  321. *   void                                                                     *
  322. *                                                                            *
  323. * KEYWORDS:                                                                  *
  324. *****************************************************************************/
  325. void DummyLinkCagdDebug(void)
  326. {
  327.     IritPrsrDbg();
  328. }
  329.  
  330. #endif /* DEBUG */
  331.